import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Random;

public class UI {
    private Frame win;
    private Label l_name;
    private Button button;
    private Random r = new Random();

    public UI() {
        win = new Frame();
        win.setLayout(new FlowLayout());
        win.setVisible(true);
        win.setBounds(100, 100, 200, 200);

        button = new Button("getName");
        l_name = new Label("name");

        win.add(l_name);
        win.add(button);
        button.addActionListener(new NameHolder());
    }

    class NameHolder implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            int n = r.nextInt(5);
            int m = 0;
            String name = null;
            File file = new File("names.txt");
            try {
                FileReader fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                while ((name = br.readLine()) != null) {
                    m = m + 1;
                    if (m == n) {
                        System.out.println(n + name);
                        l_name.setText(name);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

class TestUI {
    public static void main(String[] args) {
        new UI();
    }
}
